//
//  ShowViewController.m
//  JiuwanBox
//
//  Created by lanou3g on 15/5/28.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "ShowVieController.h"
#import "Pictures.h"
#import "UIImageView+WebCache.h"
#define ITEM_SPACING 200
@interface ShowVieController ()
{
    NSInteger t;
    NSInteger index1;
    NSInteger n;
    
}
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation ShowVieController

@synthesize carousel;
@synthesize navItem;
@synthesize wrap;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        wrap = YES;
    }
    
    return self;
}

-(void)loadView
{
    carousel = [[iCarousel alloc]init];
    carousel.frame = CGRectMake(0, 64, 320, 480);
    self.view = carousel;

}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    

    self.view.backgroundColor = [UIColor colorWithRed:0.4 green:0.6 blue:0.8 alpha:1];
    self.dataArray = [NSMutableArray array];
    
    [self read];
    
  
    carousel.delegate = self;
    carousel.dataSource = self;
    
    carousel.type = iCarouselTypeCoverFlow;
    self.navigationItem.title = @"封面展示";
    
    UIBarButtonItem * rightIB = [[UIBarButtonItem alloc]initWithTitle:@"展示类型" style:(UIBarButtonItemStylePlain) target:self action:@selector(switchCarouselType)];
    self.navigationItem.rightBarButtonItem = rightIB;
    
    
    
    
}





-(void)read
{
    NSString *string = [NSString stringWithFormat:@"http://box.dwstatic.com/apiAlbum.php?action=d&albumId=%@",self.model.galleryId];
    NSURL *url = [NSURL URLWithString:string];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60];
    
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    NSArray * sourceArray = [dic  objectForKey:@"pictures"];
    for (NSDictionary *dic  in sourceArray) {
        Pictures *picture=[[Pictures alloc]init];
        [picture setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:picture];
    }
}







- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.carousel = nil;
    self.navItem = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)switchCarouselType
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择类型" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"直线", @"圆圈", @"反向圆圈", @"圆桶", @"反向圆桶", @"封面展示", @"封面展示2", @"纸牌", nil];
    
    [sheet showInView:self.view];
    
}

- (void)toggleWrap
{
	
}


#pragma mark -

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    for (UIView *view in carousel.visibleItemViews)
    {
        view.alpha = 1.0;
    }
    
    [UIView beginAnimations:nil context:nil];
    carousel.type = buttonIndex;
    [UIView commitAnimations];
    
    navItem.title = [actionSheet buttonTitleAtIndex:buttonIndex];
}

#pragma mark -

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    return _dataArray.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index
{

    UIView *view = [[UIView alloc]init] ;
    
    view.frame = CGRectMake(70, 80, 180, 300);
    Pictures *picture = _dataArray[index];
    UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 180, 260)];
//    imageView.frame = view.frame;
    [imageView sd_setImageWithURL:[NSURL URLWithString:picture.coverUrl]];
//    view = imageView;
	
    self.label = [[UILabel alloc]initWithFrame:CGRectMake(0, 280, 180, 20)];
    _label.textAlignment = 1;
    _label.textColor = [UIColor blackColor];
	_label.text = picture.picDesc;
    [view addSubview:_label];
	[view addSubview:imageView];
    
    return view;
}

- (NSUInteger)numberOfPlaceholdersInCarousel:(iCarousel *)carousel
{
    return 0;
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel
{
    return _dataArray.count;
}

- (CGFloat)carouselItemWidth:(iCarousel *)carousel
{
    return ITEM_SPACING;
}

- (CATransform3D)carousel:(iCarousel *)_carousel transformForItemView:(UIView *)view withOffset:(CGFloat)offset
{
    view.alpha = 1.0 - fminf(fmaxf(offset, 0.0), 1.0);
    
    CATransform3D transform = CATransform3DIdentity;
    transform.m34 = self.carousel.perspective;
    transform = CATransform3DRotate(transform, M_PI / 8.0, 0, 1.0, 0);
    return CATransform3DTranslate(transform, 0.0, 0.0, offset * carousel.itemWidth);
}

-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{

    index1 =index;
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"保存图片",@"举报", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    actionSheet.tag = 101;
    
    [actionSheet showInView:self.view];

}



-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (actionSheet.tag == 101) {
        if (buttonIndex == 0) {
            NSLog(@"保存图片");
            Pictures *pictures = _dataArray[index1];
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pictures.coverUrl]]];
            
            
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            
        }
        
        if (buttonIndex == 2) {
            
            NSLog(@"取消");
            
        }
        
        if (buttonIndex == 1) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
                                                            message:@"举报成功"
                                  
                                                           delegate:self
                                  
                                                  cancelButtonTitle:@"确定"
                                  
                                                  otherButtonTitles:nil];
            
            [alert show];
            
        }

    }
}


- (void)image: (UIImage *)image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo

{
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg = @"保存图片失败" ;
        
    }else{
        msg = @"保存图片成功" ;
        
    }
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"保存图片结果提示"
                          
                                                    message:msg
                          
                                                   delegate:self
                          
                                          cancelButtonTitle:@"确定"
                          
                                          otherButtonTitles:nil];
    
    [alert show];
}


- (BOOL)carouselShouldWrap:(iCarousel *)carousel
{
    return wrap;
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
